﻿using DevExpress.XtraGrid.Demos.util;
using DiaoPaiDaYin;
using DXApplicationTangche.Utils;
using mendian;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.service
{
    /// <summary>
    /// 图片服务
    /// </summary>
    public class PictureService
    {
        /// <summary>
        /// 下载没有的款式图片
        /// </summary>
        /// <param name="difflist"></param>
        public static void DownloadDifferentPic(List<StylePic> difflist)
        {
            foreach (StylePic sp in difflist)
            {
                if (sp.stylePicN != "")
                {
                    try
                    {
                        FileUtil.DownloadPicture(sp.stylePicURL, @"pic\" + sp.stylePicN, -1);
                    }
                    catch
                    {

                    }
                }
            }
        }

        /// <summary>
        /// 下载没有的设计点图片
        /// </summary>
        /// <param name="difflist"></param>
        public static void DownloadSheJiDianPic(List<SheJiDianPic> difflist)
        {
            foreach (SheJiDianPic sjdp in difflist)
            {
                if (sjdp.picn != "")
                {
                    try
                    {
                        FileUtil.DownloadPicture(sjdp.picurl, @"pic\" + sjdp.picn, -1);
                        //DownloadPic(sjdp.picurl, @"pic\", sjdp.picn);
                    }
                    catch
                    {

                    }
                }
            }
        }

        /// <summary>
        /// 下载没有的面料图片
        /// </summary>
        /// <param name="difflist"></param>
        public static void DownloadMianliaoPic(List<MianLiaoPic> difflist)
        {
            foreach (MianLiaoPic sp in difflist)
            {
                if (sp.picn != "")
                {
                    try
                    {
                        FileUtil.DownloadPicture(sp.picurl, @"pic\" + sp.picn, -1);
                    }
                    catch
                    {

                    }
                }
            }
        }


        private static bool SaveBinaryFile(WebResponse response, string savePath)
        {
            bool value = false;
            byte[] buffer = new byte[1024];
            Stream outStream = null;
            Stream inStream = null;
            try
            {
                if (File.Exists(savePath)) File.Delete(savePath);
                outStream = System.IO.File.Create(savePath);
                inStream = response.GetResponseStream();
                int l;
                do
                {
                    l = inStream.Read(buffer, 0, buffer.Length);
                    if (l > 0) outStream.Write(buffer, 0, l);
                } while (l > 0);
                value = true;
            }
            finally
            {
                if (outStream != null) outStream.Close();
                if (inStream != null) inStream.Close();
            }
            return value;
        }

    }
}
